package controller;

import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import entity.User;
import org.springframework.web.bind.annotation.*;
import vo.AjaxResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;

/**
 * Author：汤小洋
 * Date：2023-07-05 16:24
 * Description：<描述>
 */
@RestController
public class ParamController {

    @RequestMapping("/param1")
    public void param1(HttpServletRequest req, HttpServletResponse res, HttpSession session) throws IOException {
        System.out.println(req.getParameter("name"));
        System.out.println(req.getHeader("User-Agent"));
        System.out.println(req.getServletContext().getRealPath("upload"));
        System.out.println(session.getId());

        res.setContentType("text/html;charset=utf-8");
        PrintWriter out = res.getWriter();
        out.print("<h2>成功</h2>");
        out.close();
    }

    @RequestMapping("/param2")
    public AjaxResult param2(@RequestParam("name") String name,@RequestParam(required = false,defaultValue = "18") int age,double height){
        System.out.println(name);
        System.out.println(age);
        System.out.println(height);
        return AjaxResult.success();
    }

    @RequestMapping("/param3/{id}/{name}")
    public AjaxResult param3(@PathVariable("id") Integer id, @PathVariable String name){
        System.out.println(id);
        System.out.println(name);
        return AjaxResult.success();
    }

    @RequestMapping("/param4")
    public AjaxResult param4(@RequestHeader("User-Agent") String userAgent){
        System.out.println(userAgent);
        UserAgent agent = UserAgentUtil.parse(userAgent);
        System.out.println(agent.getOs().getName());
        System.out.println(agent.getBrowser().getName());
        return AjaxResult.success();
    }

    @RequestMapping("/param5")
    public AjaxResult param5(@CookieValue("JSESSIONID") String sessionId){
        System.out.println(sessionId);
        return AjaxResult.success();
    }

    @RequestMapping("/param6")
    public AjaxResult param6(@RequestBody User user){
    // public AjaxResult param6(@RequestBody Integer[] ids){
    // public AjaxResult param6(@RequestBody List<User> users){
    // public AjaxResult param6(@RequestBody String str){
    //     System.out.println(str);
    //     JSONObject obj = JSONUtil.parseObj(str);
    //     System.out.println(obj.get("id"));
        System.out.println(user);
        return AjaxResult.success();
    }

    @RequestMapping("/param7")
    public AjaxResult param7(User user){
        System.out.println(user);
        return AjaxResult.success(user);
    }
}
